<?php namespace App\Repositories\Eloquent\Item;

use App\Exceptions\OtherException;
use App\Models\Company;
use App\Repositories\Item\CompanyRepositoryInterface;

class CompanyRepository implements CompanyRepositoryInterface
{
    /**
     * 获取公司
     *
     * @param $id
     *
     * @return \Illuminate\Database\Eloquent\Collection|\App\Models\Company|\App\Models\Company[]
     */
    public function show($id)
    {
        return Company::query()->find($id);
    }

    /**
     * 新增公司
     *
     * @param $creator_uid
     * @param $name
     *
     * @return Company
     */
    public function store($creator_uid, $name)
    {
        $bool = $this->checkNameRepeat($creator_uid, $name);
        throw_if($bool, new OtherException('公司名称重复'));

        $model = new Company(compact('creator_uid', 'name'));
        throw_unless($model->save(), new OtherException('创建公司失败，请重试'));

        return $model;
    }

    /**
     * 修改公司信息
     *
     * @param $id
     * @param $name
     * @param $creator_uid
     *
     * @return Company|Company[]|\Illuminate\Database\Eloquent\Collection
     */
    public function update($id, $name, $creator_uid)
    {
        throw_unless($this->isMyCompany($creator_uid, (int)$id), new OtherException('没有权限修改'));

        $bool = $this->checkNameRepeat($creator_uid, $name, $id);
        throw_if($bool, new OtherException('公司名称重复'));

        $company = $this->show((int)$id);
        $company->name = $name;
        throw_unless($company->save(), new OtherException('公司修改失败，请重试!'));

        return $company;
    }

    /**
     * 获取用户公司
     *
     * @param     $creator_uid
     * @param int $per_page
     *
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function lists($creator_uid, $per_page = 10)
    {
        return Company::query()
            ->where(compact('creator_uid'))
            ->latest()
            ->paginate($per_page);
    }

    /**
     * 判断用户是否拥有该公司
     *
     * @param $user_id
     * @param $company_id
     *
     * @return bool
     */
    public function checkUserHasCompany($user_id, $company_id)
    {
        return Company::query()
                ->where('creator_uid', $user_id)
                ->where('id', $company_id)
                ->count() > 0;
    }

    /**
     * 判断公司标题是否重复
     *
     * @param      $user_id
     * @param      $name
     * @param null $unless_id
     *
     * @return bool
     */
    private function checkNameRepeat($user_id, $name, $unless_id = null)
    {
        $query = Company::query();
        if (isset($unless_id)) {
            $query->where('id', '<>', $unless_id);
        }

        return $query->where('creator_uid', $user_id)
                ->where('name', $name)
                ->count() > 0;
    }

    /**
     * 是否自己的公司
     *
     * @param $creator_uid
     * @param $id
     *
     * @return bool
     */
    private function isMyCompany($creator_uid, $id)
    {
        return Company::query()->where(compact('creator_uid', 'id'))->count() > 0;
    }
}
